﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxGridView;

namespace TireService.Web.View.SalesView
{
    public partial class QuotationGenerator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            if (Page.IsPostBack != true)
            {
                // Prepare list of business companies
                ddlBusinessCustomer.DataSource = db.GetParentCustomers();
                ddlBusinessCustomer.ValueField = "CustomerId";
                ddlBusinessCustomer.TextField = "CompanyName";
                ddlBusinessCustomer.DataBind();
            }
            int? CustomerId = null;

            if (ddlBusinessCustomer.Value != null)
            {
                CustomerId = Convert.ToInt32(CustomerId);
            }

            BindTireSetTargetGroup(db, Convert.ToInt32(ddlTireType.Value), Convert.ToInt32(rblCustomerFilter.Value), CustomerId, Convert.ToDecimal(txtVF.Text), Convert.ToDecimal(txtHF.Text), Convert.ToDecimal(txtVB.Text), Convert.ToDecimal(txtHB.Text));
        }

        private void BindTireSetTargetGroup(TireServiceDB db, int TireTypeId, int CustomerType, int? CustomerId, decimal VF, decimal HF, decimal VB, decimal HB)
        {
            List<BusinessTireSetListView> tires = new List<BusinessTireSetListView>();

            if (CustomerType == 0)
            {
                // Private customers/consumers
                tires = db.BusinessTireSetListViews.Where(p => p.ParentCustomerId == null & p.IsCompany == false & p.TireTypeId == TireTypeId & p.TireSetStatusId == (int)Shared.TireSetStatus.OnStock & (p.LeftFrontTirePattern <= VF | p.RightFrontTirePattern <= HF | p.LeftRearTirePattern <= VB | p.RightRearTirePattern <= HB)).ToList();
                //gvTireSetTargetGroup.DataSource = db.BusinessTireSetListViews.Where(p => p.TireTypeId == TireTypeId & (p.LeftFrontTirePattern <= VF | p.RightFrontTirePattern <= HF | p.LeftRearTirePattern <= VB | p.RightRearTirePattern <= HB)).ToList(); 
                //db.BusinessTireSetListViews.First().
            }
            if (CustomerType == 1)
            {
                // Business customers
                tires = db.BusinessTireSetListViews.Where(p => (p.CustomerId == CustomerId | p.ParentCustomerId == CustomerId) & p.TireTypeId == TireTypeId & p.TireSetStatusId == (int)Shared.TireSetStatus.OnStock & (p.LeftFrontTirePattern <= VF | p.RightFrontTirePattern <= HF | p.LeftRearTirePattern <= VB | p.RightRearTirePattern <= HB)).ToList();
                //gvTireSetTargetGroup.DataSource = db.BusinessTireSetListViews.Where(p => (p.CustomerId == CustomerId | p.ParentCustomerId == CustomerId) & p.TireTypeId == TireTypeId & (p.LeftFrontTirePattern <= VF | p.RightFrontTirePattern <= HF | p.LeftRearTirePattern <= VB | p.RightRearTirePattern <= HB)).ToList(); 
            }         

            // Get open quotations to check for need of excluding in quotation generator
            List<TireQuotation> activequotations = db.GetTireQuotationActive();

            if (activequotations.Count > 0)
            {
                foreach (TireQuotation activequotation in activequotations)
                {
                    foreach (BusinessTireSetListView tire in tires)
                    {
                        if (activequotation.TireSetId == tire.TireSetId)
                        {
                            // Quotation already exist - remove from tire list
                            tires.Remove(tire);
                        }
                    } 
                }
            }

            gvTireSetTargetGroup.DataSource = tires;
            gvTireSetTargetGroup.DataBind();

            // Grouping
            gvTireSetTargetGroup.ClearSort();
            gvTireSetTargetGroup.GroupSummary.Clear();
            gvTireSetTargetGroup.GroupBy(gvTireSetTargetGroup.Columns["Width"]);
            gvTireSetTargetGroup.GroupBy(gvTireSetTargetGroup.Columns["Height"]);
            gvTireSetTargetGroup.GroupBy(gvTireSetTargetGroup.Columns["Size"]);
            gvTireSetTargetGroup.GroupBy(gvTireSetTargetGroup.Columns["TireLoadIndexDescription"]);
            gvTireSetTargetGroup.GroupBy(gvTireSetTargetGroup.Columns["TireSpeedIndexDescription"]);

            gvTireSetTargetGroup.ExpandAll();
        }

        protected void gvTireSetTargetGroup_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            // Values 0: TireTypeId, 1: VF, 2: HF, 3: VB, 4: HB, 5: CustomerFilter(0 or 1), 6: CustomerId
            string[] param = e.Parameters.Split('|');

            TireServiceDB db = new TireServiceDB();

            int? CustomerId = null;
            if (param[6] != null & param[6] != "" & param[6] != "null")
	        {
		        CustomerId = Convert.ToInt32(param[6]);
	        }

            BindTireSetTargetGroup(db, Convert.ToInt32(param[0]), Convert.ToInt32(param[5]), CustomerId, Convert.ToDecimal(param[1]), Convert.ToDecimal(param[2]), Convert.ToDecimal(param[3]), Convert.ToDecimal(param[4]));
        }

        protected void gvTireSetTargetGroup_CustomUnboundColumnData(object sender, ASPxGridViewColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "ReplaceQuantity")
            {
                int VF = Convert.ToInt32(e.GetListSourceFieldValue("LeftFrontTirePattern"));
                int HF = Convert.ToInt32(e.GetListSourceFieldValue("RightFrontTirePattern"));
                int VB = Convert.ToInt32(e.GetListSourceFieldValue("LeftRearTirePattern"));
                int HB = Convert.ToInt32(e.GetListSourceFieldValue("RightRearTirePattern"));

                int replacequantity = 0;
                if (VF <= Convert.ToInt32(txtVF.Text))
                {
                    replacequantity += 1;
                }
                if (HF <= Convert.ToInt32(txtHF.Text))
                {
                    replacequantity += 1;
                }
                if (VB <= Convert.ToInt32(txtVB.Text))
                {
                    replacequantity += 1;
                }
                if (HB <= Convert.ToInt32(txtHB.Text))
                {
                    replacequantity += 1;
                }

                e.Value = replacequantity;
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            gvExport.WriteXlsToResponse();
        }

    }
}